package com.gsn.poker.play;

import java.util.List;

import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.equations.Bounce;
import aurelienribon.tweenengine.equations.Expo;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.Remove;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Scaling;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnAnimation;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.template.GsnBubbleChat;
import com.gsn.engine.tween.ActorAccessor;
import com.gsn.engine.tween.GsnTween;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.logic.Card;
import com.gsn.poker.logic.CardGroupLogic;
import com.gsn.poker.logic.GameRule;

public class PlayerGroup extends Group {
	private static final String tag = "Player Group";
	protected boolean available;
	AvatarUser avatar;
	public CardGroup cardGroup;

	int id;
	int pad = 2;
	private BoardLayer boardLayer;
	public Image boss;
	public boolean viewToUser = false;
	public UserInfo info;
	// Image effDenLuotImg;
	int type;

	public PlayerGroup(int type, BoardLayer boardLayer, int id) {
		transform = GsnGame.transform;
		this.id = id;
		this.type = type;
		this.boardLayer = boardLayer;

		avatar = new AvatarUser(type);
		cardGroup = new CardGroup();
		boss = new Image(PokerTexture.inst().iconHost);
		Image bg = new Image(PokerTexture.inst().bgAvatar);
		// boss.visible = false;
		pad = (int) bg.width / 20;
		this.width = bg.width;
		this.height = bg.height;

		if (type == 0 || type == -1) {
			ActorUtility.setRatio(avatar, 0f, 0f, pad, this.height * 0.2f);
			ActorUtility.setRatio(cardGroup, 1f, 1.0f, 0.98f * width, height * 0.92f);
			ActorUtility.setRatio(boss, 0, 1f, 0, height);
		} else {
			ActorUtility.setRatio(avatar, 1, 0f, width - pad, this.height * 0.2f);
			ActorUtility.setRatio(cardGroup, 0, 1.0f, width * 0.02f, height * 0.92f);
			ActorUtility.setRatio(boss, 1f, 1f, width, height);
		}

		addActor(bg);
		addActor(avatar);
		addActor(cardGroup);
		addActor(boss);

		setAvailable(false);
	}

	public boolean isAvailable() {
		return available;
	}

	public void setCardID(int stt, int id) {
		// Gdx.app.log("Show first card  ", " " + stt + "   " + id );

		cardGroup.cards[stt].visible = true;
		cardGroup.data[stt] = true;
		// boolean ok = cardGroup.cards[stt].color.r == 1;
		cardGroup.cards[stt].setId(id);

		if (cardGroup.cards[stt].getId() != 52) {
			// cardGroup.cards[stt].color.set(0.5f, 0.5f, 0.5f, 1f);
			// Tween.to(cardGroup.cards[stt], ActorAccessor.TINT,
			// 0.3f).target(1, 1, 1).ease(Sine.IN).start(GsnTween.manager);
		}

	}

	public int getCardID(int stt) {
		return cardGroup.cards[stt].getId();
	}

	public void setDark(int stt, boolean dark) {
		cardGroup.cards[stt].setDark(dark);
	}

	public void addCardID(int id) {
		cardGroup.nhanBai(id, true);
	}

	public int getNumCard() {
		return cardGroup.numCard;
	}

	public void setAvailable(boolean _available) {
		available = _available;
		// visible = available;
		avatar.visible = _available;
		avatar.color.set(0.5f, 0.5f, 0.5f, 1f);
		System.out.println(" set available " + available);
	}

	public void setUserInfo(UserInfo info) {
		avatar.visible = true;
		avatar.color.set(0.7f, 0.7f, 0.7f, 0.7f);

		avatar.setGold(info.gold);
		if (info.name == null)
			info.name = "";
		avatar.setName(info.name);
		avatar.setAvatar(info.avatar);
		id = info.id;
		this.info = info;
	}

	public void nhanBai(int idCard) {
		cardGroup.nhanBai(idCard, true);
	}

	public void newGame() {
		if (viewToUser == true) {

			available = true;
		}
		if (available)
			avatar.color.set(1, 1, 1, 1);
		else
			avatar.color.set(0.5f, 0.5f, 0.5f, 0.5f);
		cardGroup.newGame();

		viewToUser = false;
		Actor a = findActor("removeImg");
		if (a != null)
			a.remove();
		System.out.println(" gan lai fold = false khi new game");
		_fold = false;
		if (moneyGroup != null)
			moneyGroup.remove();
	}

	public void effDenLuot() {
		// effDenLuotImg.visible = true;
	}

	public void effQuaLuot() {
		// effDenLuotImg.visible = false;
	}

	public void chat(String text) {
		GsnBubbleChat chatImg;
		if (type == 0 || type == -1) {
			chatImg = new GsnBubbleChat(PokerTexture.inst().bubbleChatTrai, 0.6f * width, PokerTexture.inst().fontMedium, text);
			ActorUtility.setRatio(chatImg, 0, 0, avatar.x + avatar.width, avatar.y + avatar.height);
		} else {
			chatImg = new GsnBubbleChat(PokerTexture.inst().bubbleChatPhai, 0.6f * width, PokerTexture.inst().fontMedium, text);
			ActorUtility.setRatio(chatImg, 1, 0, avatar.x, avatar.y + avatar.height);
		}
		addActor(chatImg);
		chatImg.action(Delay.$(Remove.$(), 1.5f));
	}

	public void to() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuTo, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void choiToiBen() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuChoiToiBen, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void nhuongTo() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuNhuongTo), -1.5f);
	}

	boolean _fold = false;

	public boolean isFold() {
		return _fold;
	}

	public void fold() {
		_fold = true;
		cardGroup.showImg(new Image(PokerTexture.inst().chuUpBo, Scaling.none, Align.CENTER, "removeImg"), -2f);
		for (int i = 0; i < cardGroup.numCard; i++) {
			cardGroup.cards[i].setId(52);
			cardGroup.cards[i].setDark(true);
		}
	}

	public void toGapDoi() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuToGapDoi, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void toTatCa() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuToTatCa, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void toNua() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuToMotNua, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void toMotPhanTu() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuToMotPhanTu, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void theo() {
		cardGroup.showImg(new Image(PokerTexture.inst().chuTheo, Scaling.none, Align.CENTER, "removeImg"), -1.5f);
	}

	public void updateMoney(long bean) {
		avatar.setGold(bean);
	}

	public long getMoney() {
		return avatar.getMoney();
	}

	GsnAnimation ani;
	Image chat;

	public void effectTime(float time) {
		ani = new GsnAnimation(time / PokerTexture.inst().Time.size(), PokerTexture.inst().Time);
		ActorUtility.setCenter(ani, avatar.x + avatar.width / 2, avatar.y + avatar.height / 2);
		ani.start();
		addActor(ani);
		ani.action(Delay.$(Remove.$(), time));
	}

	public void effectChat(float time, int index) {
		AtlasRegion list = null;
		switch (index) {
		case 1:
			list = PokerTexture.inst().emo1;
			break;
		case 2:
			list = PokerTexture.inst().emo2;
			break;
		case 3:
			list = PokerTexture.inst().emo3;
			break;
		case 4:
			list = PokerTexture.inst().emo4;
			break;
		case 5:
			list = PokerTexture.inst().emo5;
			break;
		case 6:
			list = PokerTexture.inst().emo6;
			break;
		case 7:
			list = PokerTexture.inst().emo7;
			break;
		case 8:
			list = PokerTexture.inst().emo8;
			break;
		case 9:
			list = PokerTexture.inst().emo9;
			break;
		case 10:
			list = PokerTexture.inst().emo10;
			break;
		case 11:
			list = PokerTexture.inst().emo11;
			break;
		case 12:
			list = PokerTexture.inst().emo12;
			break;
		case 13:
			list = PokerTexture.inst().emo13;
			break;
		case 14:
			list = PokerTexture.inst().emo14;
			break;
		case 15:
			list = PokerTexture.inst().emo15;
			break;
		case 16:
			list = PokerTexture.inst().emo16;
			break;
		case 17:
			list = PokerTexture.inst().emo17;
			break;
		case 18:
			list = PokerTexture.inst().emo18;
			break;
		case 19:
			list = PokerTexture.inst().emo19;
			break;
		case 20:
			list = PokerTexture.inst().emo20;
			break;

		default:
			break;
		}
		chat = new Image(list);
		if (type == 0)
			ActorUtility.setCenter(chat, avatar.x + avatar.width / 2, avatar.y + avatar.height / 2);
		else if (type == -1)
			ActorUtility.setCenter(chat, avatar.x + avatar.width / 2 + avatar.width / 5, avatar.y + avatar.height / 2);
		else
			ActorUtility.setCenter(chat, avatar.x + avatar.width / 2 - avatar.width / 5, avatar.y + avatar.height / 2);

		chat.scaleY = 1f;
		float dur = 0.5f;
		// Tween.to(img, ActorAccessor.SCALE_XY, time).target(1f,
		// 1.1f).ease(Bounce.OUT).start(GsnTween.manager);

		Timeline.createSequence().push(Tween.to(chat, ActorAccessor.SCALE_XY, dur).target(1f, 0.8f).ease(Bounce.OUT)).push(Tween.to(chat, ActorAccessor.SCALE_XY, dur).target(1f, 1f).ease(Expo.OUT))
		// .push(Tween.to(chat, ActorAccessor.SCALE_XY, time).target(1f,
		// 1.0f).ease(Expo.OUT))
				.repeat(5, (int) (dur * 1000)).start(MyPoker.game.tweenManger);
		// chat.setLoop(true);
		// chat.start();
		addActor(chat);
		chat.action(Delay.$(Remove.$(), time));
	}

	public void stopTime() {
		if (ani != null)
			ani.remove();
	}

	GameRule rule = new GameRule();

	private void lenBo(CardGroupLogic kq) {
		for (int kia = 0; kia < getNumCard(); kia++)
			cardGroup.cards[kia].setDark(true);

		for (int i = 0; i < kq.getCardsCount(); i++) {
			for (int kia = 0; kia < getNumCard(); kia++)
				if (cardGroup.cards[kia].getId() == kq.getCard(i).Id) {
					// cardGroup.cards[kia].lenBai();
					cardGroup.cards[kia].setDark(false);
				}

		}
	}

	CardGroupLogic group;
	CardGroupLogic groupBo;
	int groupType;

	public void timBo() {
		group = new CardGroupLogic();
		groupBo = new CardGroupLogic();

		Gdx.app.log(tag, "num card : " + cardGroup.numCard);
		for (int i = 0; i < cardGroup.numCard; i++) {
			Gdx.app.log(tag, "id : " + cardGroup.cards[i].getId());
			group.putCardIn(new Card(cardGroup.cards[i].getId()));
			// cardGroup.cards[i].setDark(false);
		}

		rule.timPhom(group, groupBo);
		for (int i = 0; i < groupBo.cards.size(); i++) {
			System.out.println(" Bo chon " + groupBo.getCard(i).Id);
		}
	}

	private boolean newCardInBo() {
		int tmp = cardGroup.cards[cardGroup.numCard - 1].getId();
		for (int i = 0; i < groupBo.getCardsCount(); i++)
			if (tmp == groupBo.getCard(i).Id)
				return true;
		return false;
	}

	public void hienBo(boolean lenbo, boolean end) {
		// timBo();
		Gdx.app.log(tag, "hien bo nguoi " + id + ": " + lenbo + " " + end);
		float time = -2f;
		CardGroupLogic g1 = new CardGroupLogic(cardGroup.cards[0].id, cardGroup.cards[1].id, cardGroup.cards[2].id, cardGroup.cards[3].id, cardGroup.cards[4].id);
		groupBo = new CardGroupLogic();
		int tmp = rule.timPhom(g1, groupBo);
		groupType = tmp;
		if (tmp != CardGroupLogic.eTHAU && lenbo)
			lenBo(groupBo);
		Gdx.app.log(tag, "type : " + tmp);
		boolean hienAnh = end || newCardInBo();
		switch (tmp) {
		case CardGroupLogic.eTHAU:
			if (end)
				cardGroup.showImg(new Image(PokerTexture.inst().chuMau, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eDOI:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuDoi, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eTHU:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuThu, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eSAM_CO:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuSamCo, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eSANH:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuSanh, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eTHUNG:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuThung, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eCU_LU:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuCulu, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eTU_QUY:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuTuQuy, Scaling.none, Align.CENTER, "removeImg"), time);
			break;
		case CardGroupLogic.eTHUNG_PHA_SANH:
			if (hienAnh)
				cardGroup.showImg(new Image(PokerTexture.inst().chuThungPhaSanh, Scaling.none, Align.CENTER, "removeImg"), time);
			if (lenbo && info.id == UserData.myId)
				boardLayer.effectThang(CardGroupLogic.eTHUNG_PHA_SANH, groupBo);
			break;
		}
	}

	public void allCard(int[] card) {
		if (card[1] == cardGroup.cards[1].getId()) {
			cardGroup.cards[0].setId(card[0]);
		} else
			cardGroup.cards[0].setId(card[1]);
		if (card.length == 5)
			cardGroup.cards[4].setId(card[4]);
	}

	Group moneyGroup;

	public void effectMoney(long money) {
		// TODO Auto-generated method stub
		moneyGroup = CongTruTienGroup.convertNum(money, PokerTexture.inst().cong, PokerTexture.inst().tru, PokerTexture.inst().daucong, PokerTexture.inst().dautru, PokerTexture.inst().dauphaycong, PokerTexture.inst().dauphaytru);
		// a.color.a = 0.3f;
		// a.scaleX = a.scaleY = 0.5f;
		// for (Tween t : GsnTween.setTintGroup(a))
		// t.target(0.3f, 0.3f, 0.3f).start(GsnTween.manager);
		ActorUtility.setRatio(moneyGroup, 0.5f, 0f, cardGroup.x + cardGroup.width / 2, cardGroup.y + cardGroup.height / 2);
		addActor(moneyGroup);
		float time = 1f;

		// for (Tween t : GsnTween.toTintGroup(a, time))
		// t.target(1, 1, 1).start(GsnTween.manager);

		// Timeline.createSequence().beginParallel().push(Tween.to(a,
		// ActorAccessor.OPACITY, time).target(1)).push(Tween.to(a,
		// ActorAccessor.SCALE_XY, time).target(1f,
		// 1f).ease(Sine.OUT)).push(Tween.to(a, ActorAccessor.CPOS_XY,
		// time).target(cardGroup.x + cardGroup.width / 2, cardGroup.y +
		// cardGroup.height).ease(Back.OUT)).end().start(GsnTween.manager);
		Tween.to(moneyGroup, ActorAccessor.CPOS_XY, time).target(cardGroup.x + cardGroup.width / 2, cardGroup.y + cardGroup.height).ease(Sine.IN).start(MyPoker.game.tweenManger);
		MyPoker.game.remove(moneyGroup, 2f);
	}

	public void win() {
		cardGroup.showResult(new RotationGroup(PokerTexture.inst().nenThang, PokerTexture.inst().chuThang, "removeImg"), 3f);
	}

	public void lose() {
		for (int kia = 0; kia < getNumCard(); kia++)
			cardGroup.cards[kia].setDark(true);
		cardGroup.showResult(new RotationGroup(PokerTexture.inst().nenThua, PokerTexture.inst().chuThua, "removeImg"), 3f);
	}

	public void setArrow(boolean isArrow) {
		cardGroup.setArrow(isArrow);
	}
}
